/**
 * ADM 2.0
 * Copyright 2014 Baidu Inc. All rights reserved.
 *
 * @ignore
 * @file 列表Action基类
 * @author maoquan(maoquan0515@163.com)
 */
define(
    function (require) {
        var util = require('er/util');
        var BaseListAction = require('ub-ria/mvc/ListAction');
        var u = require('underscore');

        /**
         * @constructor
         * @extends ub-ria/mvc/ListAction
         */
        function ListAction() {
            BaseListAction.apply(this, arguments);
        }

        util.inherits(ListAction, BaseListAction);

        /**
         * 当前Action的分组名称
         *
         * @type {string}
         * @override
         */
        ListAction.prototype.group = '';

        /**
         * 当前Action负责的实体的描述名称
         *
         * @type {string}
         * @override
         */
        ListAction.prototype.entityDescription = '';

        /**
         * 视图类型
         *
         * @type {function}
         * @override
         */
        ListAction.prototype.viewType = require('./ListView');

        /**
         * 数据模型类型
         *
         * @type {function}
         * @override
         */
        ListAction.prototype.modelType = require('./ListModel');

        /**
         * 处理重新邀请时发生的错误
         *
         * @param {FakeXHR} xhr `XMLHttpRequest`对象
         * @return {boolean} 返回`true`表示错误已经处理完毕
         */
        ListAction.prototype.handleError = function (xhr) {
            try {
                var result = util.parseJSON(xhr.responseText);
            }
            catch(e) {
                result = {
                    statusCode: 1,
                    msg: '未知错误'
                };
            }
            if (xhr.status === 409 || (result && result.statusCode == '1')) {
                this.view.showToast(result.msg);
            }
        };

        // 删除
        function deleteItem(e) {
            var options = {
                title: '警告',
                content: '删除后将不可恢复，是否确认删除？'
            };
            this.view.waitConfirm(options).thenBind(this.model.deleteById, this.model, e.args)
                .then(
                    u.bind(handleDeleteResult, this, e),
                    u.bind(this.handleError, this)
                );
        }

        /**
         * 根据删除前确认
         *
         * @param {meta.UpdateContext} context 操作的上下文对象
         * @param {Object} advice 提示对象
         * @param {string} advice.message 提示信息
         * @return {Promise}
         */
        function waitConfirmForAdvice(context, advice) {
            return this.view.waitConfirm(options);
        }
        // 处理删除成功
        function handleDeleteResult(e, data) {
            if (data.status === 0) {
                this.reload();
            }
        }


        /**
         * 初始化交互行为
         *
         * @override
         */
        ListAction.prototype.initBehavior = function () {
            this.view.on('delete', deleteItem, this);
        };

        /**
         * hack
         */
        ListAction.prototype.getLayoutChangeNotifier = function () {
            return {un: function (){}}
        };


        return ListAction;
    }
);
